package com.example.administrator.login;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by Administrator on 2016/11/30.
 */
public class Registered extends AppCompatActivity {


    private  Button getVerificatiom,confirm,returnButton;
    private EditText firstPassword,secondPassword,verification,userName;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registered);


        final EditText phoneNumber = (EditText) findViewById(R.id.edit_phoneNumber);//手机号码
        returnButton = (Button) findViewById(R.id.button_return);//返回按钮
        userName = (EditText) findViewById(R.id.edit_name);
         verification = (EditText) findViewById(R.id.edit_verification);//验证码显示框
         firstPassword = (EditText) findViewById(R.id.edit_firstPassword);//密码
         secondPassword = (EditText) findViewById(R.id.edit_secondPassword);//二次确认密码
        getVerificatiom = (Button) findViewById(R.id.button_getVerification); //获取验证码

        //获取验证码按钮监听
        getVerificatiom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //拿到号码显示框里显示的字符串，如果等于11位发送信息，否则不进行任何操作
                String phoneNUmber = phoneNumber.getText().toString();
                if (phoneNumber.length() == 11){
                    v.setEnabled(false);
                    timer.start();
                }else {

                }
            }
        });

        //确认注册按钮监听
        confirm = (Button) findViewById(R.id.button_confirm);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user_name = userName.getText().toString();
                String password1 = firstPassword.getText().toString();
                String password2 = secondPassword.getText().toString();
                if (password1.equals(password2)){
                    new AlertDialog.Builder(Registered.this).setTitle("注册成功!").setMessage("即将返回登录页面，点击'确定'返回登录页面。").setPositiveButton("确定",
                            new DialogInterface.OnClickListener(){
                                public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                    // TODO Auto-generated method stub
                                    finish();
                                }
                            }).setNegativeButton("返回",new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface Dialog,int which){
                            //取消弹框，不进行任何操作
                        }
                    }).show();
                }else {
                    new AlertDialog.Builder(Registered.this).setTitle(" 注册失败！").setMessage("两次输入密码不一致，请重新输入密码。").setNegativeButton("确定",
                            new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface Dialog,int which){

                        }
                    }).show();
                }
            }
        });

        //返回按钮监听
        returnButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }
    //获取验证码倒计时
    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            getVerificatiom.setText(millisUntilFinished/1000 + "秒后重新发送");

        }

        @Override
        public void onFinish() {
            getVerificatiom.setEnabled(true);
            getVerificatiom.setText("发送验证码");
        }
    };

//   protected void onDestory(){
//       super.onDestroy();
//       timer.cancel();
//   }

}


